package com.hivekion.system.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.TreeNode;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysStatisticsTargetClassic;
import com.hivekion.system.domain.vo.SysStatisticsTargetClassicCreateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsTargetClassicUpdateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsTargetClassicViewVo;
import com.hivekion.system.service.ISysStatisticsTargetClassicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/statisticsTargetClassic")
public class SysStatisticsTargetClassicController {
      @Autowired
      ISysStatisticsTargetClassicService statisticsTargetClassicService;

      @GetMapping("/getList")
      @AutoLog(value = "获取指标分类列表", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标分类管理")
      public List<SysStatisticsTargetClassicViewVo> getList() {
	    return statisticsTargetClassicService.getList();
      }

      @GetMapping("/getTreeSelect")
      @AutoLog(value = "获取指标分类树结构", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标分类管理")
      public List<TreeNode> getTreeSelect() {
	    return statisticsTargetClassicService.getTreeSelect();
      }

      @PostMapping("/add")
      @PreAuthorize("@Permission.hasPermi('target:add')")
      @AutoLog(value = "新增指标分类", operationType = OperationTypeEnum.INSERT, module = "系统管理/指标分类管理")
      public boolean add(@RequestBody SysStatisticsTargetClassicCreateInputVo inputVo) {
	    if (statisticsTargetClassicService.checkTargetClassicNameUnique(inputVo.getName(), "")) {
		  throw new BusinessException(500, "统计指标分类名称重复");
	    }
	    return statisticsTargetClassicService.create(inputVo);
      }

      @PutMapping("/edit")
      @PreAuthorize("@Permission.hasPermi('target:edit')")
      @AutoLog(value = "更改指标分类信息", operationType = OperationTypeEnum.UPDATE, module = "系统管理/指标分类管理")
      public boolean update(@RequestBody SysStatisticsTargetClassicUpdateInputVo inputVo) {

	    if (!statisticsTargetClassicService.checkTargetClassicNameUnique(
		    inputVo.getName(), inputVo.getId())) {
		  throw new BusinessException(500, "指标分类不存在");
	    }
	    return statisticsTargetClassicService.update(inputVo);
      }

      @GetMapping("/getInfo")
      @AutoLog(value = "获取单个指标分类信息", operationType = OperationTypeEnum.SELECT, module = "系统管理/指标分类管理")
      public SysStatisticsTargetClassic getInfo(String targetClassId) {
	    return statisticsTargetClassicService.getInfo(targetClassId);
      }

      ;

      @PostMapping("/checkNameUnique")
      public boolean checkNameUnique(String itemName, String itemId) {
	    return statisticsTargetClassicService.checkTargetClassicNameUnique(itemName, itemId);
      }

      @DeleteMapping("/remove")
      @PreAuthorize("@Permission.hasPermi('target:edit')")
      @AutoLog(value = "删除指标分类", operationType = OperationTypeEnum.DELETE, module = "系统管理/指标分类管理")
      public boolean delete(String targetClassicId) {
	    // TODO 需要删除 统计项和子项的所有数据
	    return statisticsTargetClassicService.delete(targetClassicId);
      }
}
